import { App, mall } from '../../../../library/api/index'

Page({
  data: {
    navMall: 'on',
    pid: '',
    products: [],
    product: {},
    tip: 'blank',
    tipData: {}
  },
  onLoad: function(e) {
    this.setData({
      pid: e.pid
    })
    App.jice.onLoad(e)
  },
  onReady: function() {
    this.get()
    App.jice.track('page', {
      pagepath: '/pages/mall/package/view/index',
      pageTitle: '待产包-查看商品详情',
      pageType: 'view'
    })
    App.logger({
      type: App.G.view,
      channel: App.G.channel,
      target: '待产包-查看商品详情'
    })
  },
  onShow: function() {
    wx.getStorage({
      key: 'products',
      success: (res) => {
        this.setData({
          products: res.data
        })
      }
    })
    App.jice.track('appshow')
  },
  onHide: function() {
  },
  get: function() {
    wx.showToast({
      title: '正在加载',
      icon: 'loading',
      duration: 10000
    })
    mall.getProduct(this.data.pid, r => {
      wx.hideToast()
      if(r.code == 200) {
        this.setData({
          product: r.data
        })
        this.reloadCart()
      } else {
        this.showErrorTip(r.data)
      }
    }, r => {
      this.showErrorTip(r.data)
    })
  },
  addCart: function(e) {
    var pid = e.currentTarget.dataset.pid
    var items = wx.getStorageSync('cart_items')
    if(!items) {
      items = []
      items.push(pid)
    } else {
      let isAdded = false
      let isRedeem = false
      for(let i = 0; i < items.length; i++) {
        if(items[i] == pid) {
          isAdded = true
          break
        }
      }
      for(let i = 0; i < this.data.products.length; i++) {
        if(this.data.products[i].id == pid && this.data.products[i].type == 2) {
          isRedeem = true
          break
        }
      }
      if(!isAdded && !isRedeem) {
        items.push(pid)
      }
      // 排除已经添加的,先删除全部提货券然后添加添加当前提货券
      if(!isAdded && isRedeem) {
        for(let m = 0; m < items.length; m++) {
          for(let n = 0; n < this.data.products.length; n++) {
            if(items[m] == this.data.products[n].id && this.data.products[n].type == 2) {
              items.splice(m, 1)
              break
            }
          }
        }
        items.push(pid)
      }
    }
    wx.setStorageSync('cart_items', items)
    this.reloadCart()
  },
  removeCart: function(e) {
    var pid = e.currentTarget.dataset.pid
    var items = wx.getStorageSync('cart_items')
    if(items) {
      for(let i = 0; i < items.length; i++) {
        if(items[i] == pid) {
          items.splice(i, 1)
          break
        }
      }
    }
    wx.setStorageSync('cart_items', items)
    this.reloadCart()
  },
  reloadCart: function() {
    var items = wx.getStorageSync('cart_items')
    this.data.product['is_add'] = false
    for(let n = 0; n < items.length; n++) {
      if(items[n] == this.data.pid) {
        this.data.product['is_add'] = true
      }
    }
    this.setData({
      product: this.data.product
    })
  },
  showErrorTip: function(message) {
    this.setData({
      tip: 'error_tip',
      tipData: message
    })
  },
  closeTip: function() {
    this.setData({
      tip: 'blank',
      tipData: {}
    })
  }
})
